﻿using System;
using MugenMvvmToolkit.Interfaces;
using MugenMvvmToolkit.Interfaces.ViewModels;
using MugenMvvmToolkit.Interfaces.Views;

namespace MugenMvvmToolkitTest.TestInfrastructure
{
    public class TestViewManager : IViewManager
    {
        #region Properties

        public Func<IViewModel, string, IView> GetViewDelegate { get; set; }

        public Func<IInitializationContext, bool> InitializeViewDelegate { get; set; }

        public Action<IViewModel, IView> InitializeViewForViewModelDelegate { get; set; }

        public Action<IViewModel> CleanupViewOnlyVmDelegate { get; set; }

        public Action<IViewModel, IView> CleanupViewDelegate { get; set; }

        #endregion

        #region Implementation of IViewManager

        /// <summary>
        ///     Gets an instance of <see cref="IView" /> for the specified view model.
        /// </summary>
        /// <param name="viewModel">The view model which is now initialized.</param>
        /// <param name="viewBindingName">The view binding name, if any.</param>
        /// <returns>
        ///     An instance of <see cref="IView" />.
        /// </returns>
        IView IViewManager.GetView(IViewModel viewModel, string viewBindingName)
        {
            return GetViewDelegate(viewModel, viewBindingName);
        }

        /// <summary>
        ///     Configures the specified view for the specified view-model.
        /// </summary>
        /// <param name="initializationContext">
        ///     The specified <see cref="IInitializationContext" />.
        /// </param>
        bool IViewManager.InitializeView(IInitializationContext initializationContext)
        {
            if (InitializeViewDelegate != null)
                return InitializeViewDelegate(initializationContext);
            return false;
        }

        /// <summary>
        ///     Configures the specified view for the specified view-model.
        /// </summary>
        /// <param name="viewModel">The specified view model.</param>
        /// <param name="view">The specified view.</param>
        void IViewManager.InitializeView(IViewModel viewModel, IView view)
        {
            if (InitializeViewForViewModelDelegate != null)
                InitializeViewForViewModelDelegate(viewModel, view);
        }

        /// <summary>
        ///     Clears view in the specified view-model
        /// </summary>
        /// <param name="viewModel">The specified view model.</param>
        public void CleanupView(IViewModel viewModel)
        {
            if (CleanupViewOnlyVmDelegate != null)
                CleanupViewOnlyVmDelegate(viewModel);
        }

        /// <summary>
        ///     Clears view in the specified view-model
        /// </summary>
        /// <param name="viewModel">The specified view model.</param>
        /// <param name="view">The specified view.</param>
        void IViewManager.CleanupView(IViewModel viewModel, IView view)
        {
            if (CleanupViewDelegate != null)
                CleanupViewDelegate(viewModel, view);
        }

        #endregion
    }
}